package com.situ.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/lastAccessTime")
public class LastAccessTimeServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//
		
		Date date = new Date();
		System.out.println(date);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd/hh:mm:ss");
		String time = simpleDateFormat.format(date);
		System.out.println(time);
		Cookie cookie = new Cookie("lastAccessTime", time);
		resp.addCookie(cookie);
		
		Cookie[] cookies = req.getCookies();
		String lastAccessTime = "";
		if(cookies != null) {
			for(Cookie coo : cookies) {
				// lastAccessTime 时间
				if("lastAccessTime".equals(coo.getName())) {
					lastAccessTime = coo.getValue();
				}
			}
		}
		
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter writer = resp.getWriter();
		if("".equals(lastAccessTime)) {
			writer.println("您是第一次访问");
		} else {
			writer.println("您上次访问的时间：" + lastAccessTime);
		}
		
		writer.close();
	}

}
